package com.zhiyou100.crm.servlet.user;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.zhiyou100.crm.service.DepartmentService;
import com.zhiyou100.crm.service.RoleService;
import com.zhiyou100.crm.service.UserService;
import com.zhiyou100.crm.service.impl.DepartmentServiceImpl;
import com.zhiyou100.crm.service.impl.RoleServiceImpl;
import com.zhiyou100.crm.service.impl.UserServiceImpl;
import com.zhiyou100.crm.util.AdminBaseServlet;
import com.zhiyou100.crm.util.Department;
import com.zhiyou100.crm.util.Role;
import com.zhiyou100.crm.util.User;

@WebServlet(urlPatterns = { "/user/add" })
public class AddServlet extends AdminBaseServlet {
	private static final long serialVersionUID = 1L;
	UserService userService=new UserServiceImpl();
	DepartmentService departmentService=new DepartmentServiceImpl();
	RoleService roleService=new RoleServiceImpl();
	User user;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		List<Department> departments = departmentService.queryAll();
		List<Role> roles = roleService.queryRole();
		request.setAttribute("departments",departments);
		request.setAttribute("roles",roles);
		request.getRequestDispatcher("/WEB-INF/view/user/add.jsp").forward(request, response);
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		int department_id = Integer.parseInt((String)request.getParameter("department_id"));
		int role_id = Integer.parseInt((String)request.getParameter("role_id"));
		Boolean is_male = Boolean.valueOf((String) request.getParameter("is_male"));
		String mobile = request.getParameter("mobile");
		String address = request.getParameter("address");
		int age = Integer.parseInt((String)request.getParameter("age"));
		String tel = request.getParameter("tel");
		String idNum = request.getParameter("idNum");
		String email = request.getParameter("email");
		String qq = request.getParameter("qq");
		String hobby = request.getParameter("hobby");
		Integer education = Integer.valueOf((String)request.getParameter("education"));
		String cardNum = request.getParameter("cardNum");
		String nation = request.getParameter("nation");
		Integer marry = Integer.valueOf((String)request.getParameter("marry"));
		String remark = request.getParameter("remark");
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long l = System.currentTimeMillis();
		String format = df.format(new Date(l));
		Timestamp time=Timestamp.valueOf(format);
		user =new User(username,password,department_id,role_id,is_male,mobile,address,age,tel,idNum,email,qq,hobby,education,cardNum,nation,marry,remark,time);
		int b=userService.addUser(user);
		if(b>0) {
			response.sendRedirect(request.getContextPath() + "/user/list");
		}else {
			response.sendRedirect(request.getContextPath() + "list.jsp");
		}
	}

}
